package com.huya.k12.translate.model.mapper;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.spec.EncodedKeySpec;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.RowMapper;

import com.huya.k12.translate.model.entity.Word;

public class WordRowMapper implements RowMapper<Word> {

	private static String encode = "utf-8";

	@Override
	public Word mapRow(ResultSet rs, int rowNum) throws SQLException {
		// TODO Auto-generated method stub
		Word word = new Word();
		word.setId(rs.getInt("id"));
		word.setWordId(rs.getString("wordId"));
		word.setFileName(rs.getString("fileName"));

		word.setEn(strDecode(rs.getString("en")));
		word.setAr(strDecode(rs.getString("ar")));
		word.setDe(strDecode(rs.getString("de")));
		word.setEs(strDecode(rs.getString("es")));
		word.setFr(strDecode(rs.getString("fr")));
		word.setHi(strDecode(rs.getString("hi")));
		word.setIndia(strDecode(rs.getString("india")));
		word.setIt(strDecode(rs.getString("it")));
		word.setJa(strDecode(rs.getString("ja")));
		word.setKo(strDecode(rs.getString("ko")));
		word.setMs(strDecode(rs.getString("ms")));
		word.setPt(strDecode(rs.getString("pt")));
		word.setRu(strDecode(rs.getString("ru")));
		word.setTh(strDecode(rs.getString("th")));
		word.setTl(strDecode(rs.getString("tl")));
		word.setTr(strDecode(rs.getString("tr")));
		word.setUr_rPK(strDecode(rs.getString("ur_rPK")));
		word.setVi(strDecode(rs.getString("vi")));
		word.setZh(strDecode(rs.getString("zh")));
		word.setZh_rCN(strDecode(rs.getString("zh_rCN")));
		word.setZh_rSG(strDecode(rs.getString("zh_rSG")));

		word.setCreateTime(rs.getDate("createTime"));
		return word;
	}

	private String strDecode(String str) {
		if (StringUtils.isNotBlank(str)) {
			try {
				return URLDecoder.decode(str, encode);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
		return null;
	}

}
